package com.jianzh5.action.web;

import com.jianzh5.common.DataPage;
import com.jianzh5.common.DataTables;
import com.jianzh5.common.ResultJson;
import com.jianzh5.pojo.PRole;
import com.jianzh5.service.RoleServiceI;
import com.jianzh5.util.InfoCenterHome;
import com.jianzh5.util.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * 角色管理控制器
 * @author jianzh5
 *
 */
@Controller
@RequestMapping("/sys/role")
public class RoleAction {
	
	@Autowired
	private RoleServiceI roleService;
	
	@RequestMapping("/rolePage")
	@RequiresPermissions("sys:role:view")
	public String rolePage(){
		return InfoCenterHome.SERVLET_ADMIN_ROOT+"/sys/roleList";
	}
	
	@RequiresPermissions("sys:role:view")
	@RequestMapping("/roleList")
	@ResponseBody
	public DataTables roleList(DataPage datapage, HttpServletRequest request){
		String sindex = request.getParameter("order[0][column]");
		String order = request.getParameter("order[0][dir]");
		String sort = request.getParameter("columns["+sindex+"][data]");
		DataTables dt = roleService.dataTables(order,sort,datapage);
		return dt;
	}
	
	/**
	 * 跳转查看当前角色页面
	 * 查询出当前角色所拥有的资源集合
	 * 查询出系统所有资源
	 * @param roleId
	 * @param model
	 * @return
	 */
	@RequestMapping("/resourcesView")
	@RequiresPermissions("sys:role:view")
	public String resourcesView(int roleId,Model model){
		model.addAttribute("Prole", roleService.getRole(roleId));
		model.addAttribute("roleTree", UserUtils.getResourcesTree()); 
		return InfoCenterHome.SERVLET_ADMIN_ROOT+"/sys/roleView";
	}
	
	
	@RequestMapping("/roleDel")
	@RequiresPermissions("sys:role:del")
	@ResponseBody
	public ResultJson roleDel(int roleId){
		ResultJson json = new ResultJson();
		try{
			roleService.delRole(roleId);
			json.setSuccess(true);
			json.setMsg("角色删除成功...");
		}catch (Exception e) {
			json.setMsg("角色删除失败,数据库异常...");
		}
		return json;
	}
	
	@RequestMapping("/roleEditPage")
	@RequiresPermissions("sys:role:edit")
	public String roleEditPage(int roleId,Model model){
		model.addAttribute("Prole", roleService.getRole(roleId));
		model.addAttribute("roleTree", UserUtils.getResourcesTree()); 
		return InfoCenterHome.SERVLET_ADMIN_ROOT+"/sys/roleEdit";
	}
	
	/**
	 * 编辑当前角色
	 * @param prole
	 * @return
	 */
	@RequestMapping("/roleEdit")
	@ResponseBody
	@RequiresPermissions("sys:role:edit")
	public ResultJson roleEdit(PRole prole){
		ResultJson json = new ResultJson();
		try{
			roleService.editRole(prole);
			json.setSuccess(true);
			json.setMsg("角色编辑成功...");
		}catch (Exception e) {
			json.setMsg("角色编辑失败,数据库异常...");
		}
		return json;
	}
	
	
	@RequestMapping("/roleAddPage")
	@RequiresPermissions("sys:role:add")
	public String roleAddPage(Model model){
		model.addAttribute("roleTree", UserUtils.getResourcesTree()); 
		return InfoCenterHome.SERVLET_ADMIN_ROOT+"/sys/roleAdd";
	}
	
	
	@RequestMapping("/roleAdd")
	@RequiresPermissions("sys:role:add")
	@ResponseBody
	public ResultJson roleAdd(PRole prole){
		ResultJson json = new ResultJson();
		try{
			roleService.addRole(prole);
			json.setSuccess(true);
			json.setMsg("角色添加成功...");
		}catch (Exception e) {
			json.setMsg("角色添加失败，数据库异常...");
		}
		return json;
	}
	
	/**
	 * 检测当前角色是否存在
	 * @param rolename 角色名
	 * @param oldrolename 旧的角色名
	 * @return 是否存在
	 * @throws java.io.UnsupportedEncodingException
	 */
	@RequestMapping("/checkRoleName")
	@RequiresPermissions("sys:role:add")
	@ResponseBody
	public boolean checkRoleName(String rolename , String oldrolename) throws UnsupportedEncodingException{
		rolename = URLDecoder.decode(rolename, "utf-8");
		if(StringUtils.isNotBlank(oldrolename)){
			oldrolename = URLDecoder.decode(oldrolename, "utf-8");
		}
		if(rolename != null && rolename.equals(oldrolename)){
			return true;
		}else if(rolename != null && roleService.getRoleByName(rolename) == null){
			return true;
		}
		return false;
	}
}
